package fr.utbm.gi.in56.user;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.utbm.gi.in56.database.DatabaseServlet;
import fr.utbm.gi.in56.error.ErrorType;
import fr.utbm.gi.in56.notification.NotificationType;

/**
 * Servlet implementation class RegisterServlet
 * 
 * @author Adrien Hoffer & Tarik Belabbas
 */
@WebServlet( "/RegisterServlet" )
public class RegisterServlet extends DatabaseServlet
{
	/**
	 * Version number associated to the serializable class.
	 */
	private static final long	serialVersionUID	= 1L;

	/**
	 * Default constructor.
	 * 
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet()
	{
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{	
		request.setCharacterEncoding( "UTF-8" ); //$NON-NLS-1$
		
		String login = request.getParameter( "login" ); //$NON-NLS-1$
		
		if ( login.isEmpty() )
		{
			returnError( request, response, ErrorType.REGISTER_LOGIN_EMPTY );
			return;
		}
		
		String password1 = request.getParameter( "password1" ); //$NON-NLS-1$
		
		if ( password1.isEmpty() )
		{
			returnError( request, response, ErrorType.REGISTER_PASSWORD_EMPTY );
			return;
		}
		
		String password2 = request.getParameter( "password2" ); //$NON-NLS-1$
		
		if ( password1.compareTo( password2 ) != 0 )
		{
			returnError( request, response, ErrorType.REGISTER_NON_MATCHING_PASSWORDS );
			return;
		}
		
		UserManager userMg = new UserManager( getConnection() );
		
		UserBean user = new UserBean();
		user.setLogin( login );
		user.setCounty( Integer.parseInt( request.getParameter( "county" ) ) ); //$NON-NLS-1$
		
		ErrorType result = userMg.registerUser( user, password1, false );
		
		if ( result == ErrorType.NONE )
		{
			HttpSession session = request.getSession( true );
			session.setAttribute( "currentSessionUser", user ); //$NON-NLS-1$
			
			request.setAttribute( "notification", NotificationType.REGISTER_SUCCESS ); //$NON-NLS-1$
			RequestDispatcher rd = request.getRequestDispatcher("notification.jsp"); //$NON-NLS-1$
			rd.forward( request, response );
			return;
		}
		else
		{			
			returnError( request, response, result );
			return;
		}
	}
	
	/**
	 * Sets the error as request attribute and forward to previous page.
	 * 
	 * @param _error the error
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void returnError( HttpServletRequest request,
			HttpServletResponse response, ErrorType _error ) throws ServletException, IOException
	{
		request.setAttribute( "registrationError", _error ); //$NON-NLS-1$
		RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); //$NON-NLS-1$
		rd.forward( request, response );
	}
}
